package cn.zaichi.edu.excelcommon.utils.impl;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.zaichi.edu.excelcommon.entity.BaseExportParam;
import cn.zaichi.edu.excelcommon.entity.ExportTitleEntity;
import cn.zaichi.edu.excelcommon.utils.ExcelExport;
import cn.zaichi.edu.excelcommon.utils.ExportTitleUtil;
import cn.zaichi.edu.excelcommon.utils.ExportWorkbookUtil;
import cn.zaichi.edu.excelcommon.utils.ImportCellValue;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * 使用 map 方式导入excel 工具类
 */
public class ExcelMapExportUtil<T> implements ExcelExport {
    BaseExportParam param;
    Workbook workbook;
    HSSFSheet sheet;
    List<ExportTitleEntity> titleEntityList=new ArrayList<>();
    List<HashMap<String,Object>> dataList;

    List<ExportTitleEntity> lastTitleList=new ArrayList<>();

    public ExcelMapExportUtil(BaseExportParam param) throws IOException, GeneralSecurityException {
        this.param = param;
        this.param=param;
        //获取表格
        this.workbook=ExportWorkbookUtil.getWorkbook(param);
        this.sheet=ExportWorkbookUtil.getSheet(param,workbook);
        this.dataList= (List<HashMap<String, Object>>) param.getDataList();
        //设置表头数据
        setTitleEntityList();
        //创建表头
        ExportTitleUtil.setSheetTitle(param.getTitleStarRow(),titleEntityList,sheet,param,workbook);
        //设置数据
        setData();

    }


    /**
     * 设置数据
     */
    private void setData() {
        int i=param.getStartRow();
        /*插入数据*/
        for (Map<String,Object> data:dataList){
            HSSFRow row = sheet.createRow(i);
            setCell(data,row);
            i++;
        }

    }

    /**
     * 设置单元格值
     * @param data
     * @param row
     */
    private void setCell(Map<String, Object> data, HSSFRow row) {
        data.forEach((key,value)->{
            ExportTitleEntity titleEntity = lastTitleList.stream().filter(e -> e.getTitleName().equals(key)).findFirst().get();
            HSSFCell cell = row.createCell(titleEntity.getTitleSort()+param.getStartColumn());
            cell.setCellValue(value!=null?value.toString():"");
            if(param.getTempPath()==null){
                cell.setCellStyle(param.getCellStyle().getCellStyle((HSSFWorkbook) workbook));
            }
        });
    }



    /**
     * 设置表头List
     * @throws IllegalAccessException
     */
    private void setTitleEntityList(){
        if(param.getTempPath()!=null){
            List<CellRangeAddress> combineCell = ImportCellValue.getCombineCell(workbook.getSheetAt(param.getSheetNum()));
            titleEntityList=ExportTitleUtil.getTempTilteList(workbook,combineCell,param.getTitleStarRow(),param.getTitleEndRow(),param.getStartColumn());
            lastTitleList=titleEntityList;
            return;
        }
        //传入表头
        if(param.getTitleEntityList()!=null){
            titleEntityList=ExportTitleUtil.getTitleList(param.getTitleEntityList(),null);

        }else{
            titleEntityList=ExportTitleUtil.getMapTitleList(dataList.get(0));
        }
        lastTitleList=ExportTitleUtil.getLastTitle(titleEntityList);

    }

    @Override
    public HSSFWorkbook getWorkbook() {
        return (HSSFWorkbook) this.workbook;
    }

    @Override
    public void append(BaseExportParam param) {
        this.param=param;
        this.sheet=ExportWorkbookUtil.getSheet(param,workbook);
        this.dataList= (List<HashMap<String, Object>>) param.getDataList();
        //设置表头数据
        setTitleEntityList();
        //创建表头
        ExportTitleUtil.setSheetTitle(param.getTitleStarRow(),titleEntityList,sheet,param,workbook);
        //设置数据
        setData();

    }
}
